package com.neusoft.gateway.redisservice.impl;

import com.neusoft.gateway.po.ResponseBean;
import com.neusoft.gateway.redisdao.RedisDao;
import com.neusoft.gateway.redisservice.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class RedisServiceImpl implements RedisService {
    @Autowired
    private RedisDao redisDao;

    @Override
    public ResponseBean<String> set(String key, String value) {
        redisDao.set(key, value);
        return new ResponseBean<>(key);
    }

    @Override
    public ResponseBean<String> get(String key) {
        String val = redisDao.get(key);
        if (val != null) {
            return new ResponseBean<>(val);
        }
        return new ResponseBean<>(500, "未获取到key");
    }

    @Override
    public ResponseBean<String> set(String key, String value, long time, TimeUnit timeUnit) {
        redisDao.set(key, value, time, timeUnit);
        return new ResponseBean<>(key);
    }

    @Override
    public boolean isTokenBlacklisted(String token) {
        String key = "blacklist:" + token;
        return redisDao.hasKey(key);
    }

    @Override
    public void addTokenToBlacklist(String token) {
        String key = "blacklist:" + token;
        redisDao.set(key, "true", 3600, TimeUnit.SECONDS); // 令牌在黑名单中保留1小时
    }
}
